package com.wtwd.wtwddevice.listener;


import com.wtwd.device.annotation.MsgType;
import com.wtwd.wtwddevice.listener.deviceServerMsg.handler.IMsgHandler;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class BuildAnnotationTypeListener implements ApplicationListener<ApplicationReadyEvent> {

    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        System.out.println("--------------开始扫描MsgType注解并存入到Spring容器的一个Map中");
        Map<String, Object> beans = event.getApplicationContext().getBeansWithAnnotation(MsgType.class);
        ReceiveMsgServiceContext receiveMsgServiceContext = event.getApplicationContext().getBean(ReceiveMsgServiceContext.class);
        beans.forEach((name, bean) -> {
            MsgType msgType = bean.getClass().getAnnotation(MsgType.class);
            receiveMsgServiceContext.putIReceiverHandler(msgType.value().getCmdName(), (IMsgHandler) bean);
        });
    }
}